package br.unifor.cct.mia.gui;

import java.awt.Color;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.data.XYDataset;
import org.jfree.data.XYSeries;
import org.jfree.data.XYSeriesCollection;

public class Graphic extends JFrame {

	private static final long serialVersionUID = 205859364585459756L;
	private Vector nomeGrafico;
	private Vector vetorDeVetor_pontos;
	private Vector vetorDeVetor_cor;
	private Vector vetorLegenda;

	private String titulo;
	private String nomeX;
	private String nomeY;
	
	private Vector vetorSeries;
	private XYDataset dataset;
	private ChartPanel chartPanel;
	private XYPlot plot;
	private StandardXYItemRenderer renderer;
	private JFreeChart chart;

	public Graphic(String titulo, String nomX, String nomY) {
		configuraCloneDessaClasse(titulo, nomX, nomY);

		vetorSeries = new Vector();
		this.titulo = titulo;
		this.nomeX = nomX;
		this.nomeY = nomY;
		dataset = createDataset();
		chart = createChart(dataset);
		chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(300, 200));
		
		this.setTitle(".: Graphic Evaluate Fitness :.");
    	this.getContentPane().add(this.returnPanel());
    	this.pack();
    	this.setVisible(true);		
	}

	private void configuraCloneDessaClasse(String titulo, String nomX, String nomY) {
		nomeGrafico = new Vector();
		nomeGrafico.add(titulo + "@" + nomX + "@" + nomY);
		vetorDeVetor_pontos = new Vector();
		vetorDeVetor_cor = new Vector();
		vetorLegenda = new Vector();
	}

	private XYDataset createDataset() {
		XYSeriesCollection dataset = new XYSeriesCollection();
		//((XYSeriesCollection)dataset).setIntervalWidth(100);
		return dataset;
	}

	private JFreeChart createChart(XYDataset dataset) {
		JFreeChart chart = ChartFactory.createXYLineChart(
				titulo, this.nomeX, this.nomeY, dataset, PlotOrientation.VERTICAL, true, true, false);
		
		plot = chart.getXYPlot();
		renderer = (StandardXYItemRenderer) plot.getRenderer();

		return chart;
	}

	public void createLine(int posicao, String legenda) {
		vetorSeries.add(posicao, new XYSeries(legenda));
		((XYSeriesCollection) dataset).addSeries((XYSeries) vetorSeries.get(posicao));
		vetorDeVetor_pontos.add(posicao, new Vector());
		vetorDeVetor_cor.add(posicao, Color.gray);
		vetorLegenda.add(posicao, legenda);
	}

	public void addPointLinha(int posicao, double valor1, double valor2) {
		((XYSeriesCollection) this.dataset).getSeries(posicao).add(valor1, valor2);
		((Vector)vetorDeVetor_pontos.get(posicao)).add(new Double(valor1));
		((Vector)vetorDeVetor_pontos.get(posicao)).add(new Double(valor2));
	}

	public void setColorLinha(int posicao, Color cor) {
		renderer.setSeriesPaint(posicao, cor);
		vetorDeVetor_cor.set(posicao, cor);
	}

	public JPanel returnPanel() {
		return this.chartPanel;
	}
}